package cn.tedu.fruitshop.controller;

import cn.tedu.fruitshop.ex.ServiceException;
import cn.tedu.fruitshop.pojo.dto.UserAddNewDTO;
import cn.tedu.fruitshop.pojo.dto.UserLoginDTO;
import cn.tedu.fruitshop.pojo.vo.UserListItemVO;
import cn.tedu.fruitshop.pojo.vo.UserStandardVO;
import cn.tedu.fruitshop.security.LoginPrincipal;
import cn.tedu.fruitshop.service.UserService;
import cn.tedu.fruitshop.web.JsonResult;
import cn.tedu.fruitshop.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author www
 * @create 2022-11-16 15:49
 */
@Slf4j
@Validated
@Api(tags = "01.用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    //http://localhost:9081/user/login
    @ApiOperation("用户登陆")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(@Valid @RequestBody UserLoginDTO userLoginDTO){
        log.debug("开始处理登录请求，参数：{}",userLoginDTO);
        String jwt = userService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 50)
    @PostMapping("/reg")
    public JsonResult<String> reg(@Valid @RequestBody UserAddNewDTO userAddNewDTO){
        log.debug("开始处理【注册】请求，参数：{}",userAddNewDTO);
        userService.reg(userAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据用户名查找用户")
    @ApiOperationSupport(order = 50)
    @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "string")
    @GetMapping("/getUsername")
    public JsonResult getUsername(@NotNull(message = "用户名不能为空")String username){
        log.debug("开始根据名字【获取个人信息】，【当前用户名】：{}",username);
        UserStandardVO standardByUsername = userService.getStandardByUsername(username);
        if (standardByUsername != null){
            String message = "该用户名已存在，请重新输入！!";
            log.info(message);
            return JsonResult.fail(ServiceCode.ERR_CONFLICT,message);
        }
        return JsonResult.ok();
    }

    //http://localhost:9081/user/getLoginMessage
    @ApiOperation("获取用户登录信息")
    @ApiOperationSupport(order = 51)
    @GetMapping("/getLoginMessage")
    public JsonResult<LoginPrincipal> getLoginMessage(@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        log.debug("开始【获取登录信息】，【当前用户名】：{}",loginPrincipal.getUsername());
        return JsonResult.ok(loginPrincipal);
    }

    @ApiOperation("获取用户个人信息详情")
    @ApiOperationSupport(order = 52)
    @GetMapping("/{username}/selectByUsername")
    public JsonResult<UserStandardVO> personalInformation(@PathVariable String username){
        log.debug("开始根据名字【获取个人信息】，【当前用户名】：{}",username);
        UserStandardVO standardByUsername = userService.getStandardByUsername(username);
        if (standardByUsername == null){
            String message = "查询业务失败,不存在该用户!";
            return JsonResult.fail(ServiceCode.ERR_NOT_FOUND,message);
        }
        return JsonResult.ok(standardByUsername);
    }






}
